#include <stdio.h>
#include <stdlib.h>

typedef struct LNode *List;

struct LNode{
    int Data;
    List Next;
};

struct LNode L;
List Ptrl;

List findKth(int k,List Ptrl);
List insert(int pos,int val,List Ptrl);
int length(List Ptrl);

int main(){
    List p = insert(1,1,Ptrl);
    p = insert(2,2,p);
    printf("length = %d\n", length(p));
}

int length(List Ptrl){
    List p = Ptrl;
    int len = 0;
    while(p != NULL){
        p = p->Next;
        len++;
    }
    return len;
};

List findKth(int k,List Ptrl){
    List p = Ptrl;
    int i = 1;
    while( p!=NULL && i!=k){
        p = p->Next;
        i++;
    }
    if(i==k){
        return p;
    }else{
        return NULL;
    }
}

List insert(int pos,int val,List Ptrl){
    List p,s;

    if(pos == 1){
        s = (List)malloc(sizeof(struct LNode));
        s->Data = val;
        s->Next = Ptrl;
        return s;
    }
    p = findKth(pos-1, Ptrl);
    if(p!=NULL){
        s = (List)malloc(sizeof(struct LNode));
        s->Data = val;
        s->Next = p->Next;
        p->Next = s;
        return Ptrl;
    }else{
        printf("位置参数有误");
        return NULL;
    }
}
